﻿using Newtonsoft.Json;
using SYH.Domain.Applicaton;
using SYH.Domain.ViewModels.Applicaton;
using SYH.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace SYH.Web.Controllers
{
    public class BaseController : PubController
    {
        /// <summary>
        /// 如果没有登录信息则弹出登录框
        /// </summary>
        /// <param name="requestContext"></param>
        /// <param name="callback"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        protected override IAsyncResult BeginExecute(RequestContext requestContext, AsyncCallback callback, object state)
        {
            if (LogUser == null)
            {
                requestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
                requestContext.HttpContext.Response.StatusCode = 500;
                requestContext.HttpContext.Response.Write(JsonConvert.SerializeObject(new ResultVM(1, "请登录") { isLogin = 1 }));
                requestContext.HttpContext.Response.End();
                return null;
            }
            else
            {
                return base.BeginExecute(requestContext, callback, state);
            }
        }


        /// <summary>
        /// 返回当前用户登录信息
        /// </summary>
        protected LoginUserVM LogUser
        {
            get
            {
                return LoginService.LogUser;
            }
        }
    }

}